<?php
namespace Home\Controller;
use Think\Controller;
use Org\Util\Rbac;
class PublicController extends CommonController {
    
    public function login(){
        //if(!isset($_SESSION[C('USER_AUTH_KEY')])) {
            $this->display();
        //}else{
           // echo 123;exit;
            //$this->redirect('Index/index');
        //}
    }
    
    public function checklogin(){
        //此处多余可自行改为Model自动验证
        if(empty($_POST['account'])) {
            $this->error('账号必须！');
        }elseif (empty($_POST['password'])){
            $this->error('密码必须！');
        }
        $map=array();
        $map['account']=$_POST['account'];
        var_dump($map);
        //var_dump($map);
//        $map['status']=array('gt',0);
       
        //import('ORG.Util.RBAC');
        //C('USER_AUTH_MODEL','User');
        //验证账号密码
         $authInfo=RBAC::authenticate($map);
         var_dump($authInfo);
        
        if(empty($authInfo)){
            $this->error('登录失败联系管理员!');
        }else{
            if($authInfo['password']!=$_POST['password'] ){
                $this->error('登录失败联系管理员!');
            }else{
                if( $authInfo['power']<3){
                    $this->error('无权登录，请联系管理员提升权限!');
                }
                
            $_SESSION[C('USER_AUTH_KEY')]=$authInfo['id'];//记录认证标记，必须有。其他信息根据情况取用。
            
            $_SESSION['email']=$authInfo['email'];
            $_SESSION['name']=$authInfo['username'];
            $_SESSION['user']=$authInfo['account'];
            $_SESSION['id']=$authInfo['id'];
            $_SESSION['power']=$authInfo['power'];
//            $_SESSION['last_login_date']=$authInfo['last_login_date'];
//            $_SESSION['last_login_ip']=$authInfo['last_login_ip'];
            //判断是否为超级管理员
            if($authInfo['account']=='admin'){
                $_SESSION[C('ADMIN_AUTH_KEY')]=true;
            }
           //以下操作为记录本次登录信息
            $user=M('User');
            $lastdate=date('Y-m-d H:i:s');
            //$data=array();
            $id=$authInfo['id'];
            $data['last_login_time']=$lastdate;
            $data['login_ip']=$_SERVER['SERVER_ADDR'];
            var_dump($data);
            //$data['login_ip'] = 'ThinkPHP@gmail.com';
            //var_dump($data);
            $user->where("id=$id")->save($data);
            
            var_dump($_SESSION);
            RBAC::saveAccessList();//用于检测用户权限的方法,并保存到Session中
            //$this->assign('jumpUrl','/admin.php/Home/Index/index');
            $this->success('登录成功!','/YMN.php/Admin/Index/index');
            }
        }
    }
    //退出登录操作
    function logout(){
        if(!empty($_SESSION[C('USER_AUTH_KEY')])){
            unset($_SESSION[C('USER_AUTH_KEY')]);
            $_SESSION=array();
            session_destroy();
            
            
            $this->success('登出成功','/YMN.php/Home/Index/index');
        }else{
            $this->error('已经登出了');
        }
    }
    
    
    
    
    public function header(){
//      $map["id"]=$_SESSION[C('USER_AUTH_KEY')];
//      $authInfo=RBAC::authenticate($map);
//      $this->assign("user",$authInfo);
        $this->display("header");
    }
    
    public function left(){
        
//      $audit=M("article")->where("article_status=0")->count();
//      $this->assign('audit', $audit);

        $this->display("left");
    }
    
    
    
}